package com.google.gwt.mobile.phonegap.api;

import java.util.Date;

import com.google.gwt.core.client.JavaScriptObject;

/**
 * The Class Accelerometer.
 */
public class Accelerometer {

  /**
   * The Class Acceleration.
   */
  public static class Acceleration extends JavaScriptObject {
    protected Acceleration() {
    };

    /**
     * Gets the x.
     * 
     * @return the x
     */
    public native final int getX() /*-{
			return this.x;
    }-*/;

    /**
     * Gets the y.
     * 
     * @return the y
     */
    public native final int getY() /*-{
			return this.y;
    }-*/;

    /**
     * Gets the z.
     * 
     * @return the z
     */
    public native final int getZ() /*-{
			return this.z;
    }-*/;

    /**
     * Gets the time stamp.
     * 
     * @return the time stamp
     */
    public final Date getTimeStamp() {
      return new Date((long) getTimeStampNative());
    }

    private native final double getTimeStampNative() /*-{
			return this.timestamp;
    }-*/;
  }

  /**
   * The Class Options.
   */
  public static class Options {

    private final Options self = this;
    private final JavaScriptObject options = JavaScriptObject.createObject();

    /**
     * Frequency.
     * 
     * @param f the f
     * @return the options
     */
    public native Options frequency(int f) /*-{
			this.@com.google.gwt.mobile.phonegap.api.Accelerometer.Options::options.frequency = f;
			return this.@com.google.gwt.mobile.phonegap.api.Accelerometer.Options::self;
    }-*/;

    private JavaScriptObject getOptions() {
      return options;
    }

  }

  private interface Callback {
    void onSuccess(Acceleration acceleration);

    void onFailure();
  }

  /**
   * Gets the current acceleration.
   * 
   * @param callback the callback
   * @return the current acceleration
   */
  public static native void getCurrentAcceleration(Callback callback) /*-{
		$wnd.navigator.accelerometer
				.getCurrentAcceleration(
						function(acceleration) {
							callback.@com.google.gwt.mobile.phonegap.api.Accelerometer.Callback::onSuccess(Lcom/google/gwt/mobile/phonegap/api/Accelerometer$Acceleration;)(acceleration);
						},
						function() {
							callback.@com.google.gwt.mobile.phonegap.api.Accelerometer.Callback::onFailure()();
						});
  }-*/;

  /**
   * Watch acceleration.
   * 
   * @param callback the callback
   * @param options the options
   * @return the string
   */
  public static native String watchAcceleration(Callback callback, JavaScriptObject options) /*-{
		$wnd.navigator.accelerometer
				.watchAcceleration(
						function(acceleration) {
							callback.@com.google.gwt.mobile.phonegap.api.Accelerometer.Callback::onSuccess(Lcom/google/gwt/mobile/phonegap/api/Accelerometer$Acceleration;)(acceleration);
						},
						function() {
							callback.@com.google.gwt.mobile.phonegap.api.Accelerometer.Callback::onFailure()();
						}, options);
		return id;
  }-*/;

  /**
   * Watch acceleration.
   * 
   * @param callback the callback
   * @return the string
   */
  public static String watchAcceleration(final Callback callback) {
    return watchAcceleration(callback, (JavaScriptObject) null);
  }

  /**
   * Watch acceleration.
   * 
   * @param callback the callback
   * @param options the options
   * @return the string
   */
  public static String watchAcceleration(final Callback callback, final Options options) {
    return watchAcceleration(callback, options.getOptions());
  }

  /**
   * Clear watch.
   * 
   * @param watchId the watch id
   */
  public static native void clearWatch(String watchId) /*-{
		$wnd.navigator.accelerometer.clearWatch(watchId);
  }-*/;

}
